{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "%(asctime)-15s %(levelname)s %(filename)s %(lineno)d %(process)d %(message)s\n",
      "Thu 11 Mar 2021 17:28:53 WARNING <ipython-input-14-b3ab05ec2367> 34 323980 warn message\n",
      "%(asctime)-15s %(levelname)s %(filename)s %(lineno)d %(process)d %(message)s\n",
      "Thu 11 Mar 2021 17:28:53 ERROR <ipython-input-14-b3ab05ec2367> 35 323980 error message\n",
      "%(asctime)-15s %(levelname)s %(filename)s %(lineno)d %(process)d %(message)s\n",
      "Thu 11 Mar 2021 17:28:53 CRITICAL <ipython-input-14-b3ab05ec2367> 36 323980 critical message\n"
     ]
    }
   ],
   "source": [
    "# 使用最标准的方式配置log\n",
    "import logging\n",
    "import sys\n",
    "import os\n",
    "\n",
    "# create logger\n",
    "logger_name = \"example\"\n",
    "logger = logging.getLogger(logger_name)\n",
    "logger.setLevel(logging.DEBUG)\n",
    "\n",
    "\n",
    "# create file handler\n",
    "log_path = \"./log3.log\"\n",
    "fh = logging.FileHandler(log_path)\n",
    "fh.setLevel(logging.DEBUG)\n",
    "\n",
    "\n",
    "# create formatter\n",
    "fmt = \"%(asctime)-15s %(levelname)s %(filename)s %(lineno)d %(process)d %(message)s\"\n",
    "datefmt = \"%a %d %b %Y %H:%M:%S\"\n",
    "formatter = logging.Formatter(fmt, datefmt)\n",
    "\n",
    "#create StreamHandler\n",
    "sh = logging.StreamHandler()\n",
    "sh.setLevel(logging.WARNING)\n",
    "sh.setFormatter(formatter)\n",
    "\n",
    "# add handler and formatter to logger\n",
    "fh.setFormatter(formatter)\n",
    "logger.addHandler(fh)\n",
    "logger.addHandler(sh)\n",
    "# print log info\n",
    "logger.debug('debug message')\n",
    "logger.info('info message')\n",
    "logger.warning('warn message')\n",
    "logger.error('error message')\n",
    "logger.critical('critical message')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "#使用basic进行配置\n",
    "import logging\n",
    "logging.basicConfig(filename='basicConfig.log',level=logging.DEBUG)\n",
    "# logger:logging.Logger\n",
    "logging.warning('warn')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "%(asctime)-15s %(levelname)s %(filename)s %(lineno)d %(process)d %(message)s\n",
      "Thu 11 Mar 2021 17:29:33 ERROR <ipython-input-16-0558a28020c8> 12 323980 ww\n",
      "2021-03-11 17:29:33,179 ERROR <ipython-input-16-0558a28020c8> 12 323980 ww\n"
     ]
    }
   ],
   "source": [
    "import logging\n",
    "logger = logging.getLogger('example')\n",
    "logger.setLevel(logging.DEBUG)\n",
    "\n",
    "fh = logging.FileHandler('1.log')\n",
    "fh.setLevel(logging.DEBUG)\n",
    "fmt = \"%(asctime)-15s %(levelname)s %(filename)s %(lineno)d %(process)d %(message)s\"\n",
    "st = logging.StreamHandler()\n",
    "st.setFormatter(logging.Formatter(fmt))\n",
    "logger.addHandler(st)\n",
    "logger.addHandler(fh)\n",
    "logger.error('ww')\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}